package com.sunda.spmscommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmscommon.entity.SysLog;
import com.sunda.spmscommon.mapper.SysLogMapper;
import com.sunda.spmscommon.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2020-12-16
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Autowired
    SysLogMapper sysLogMapper;

    @Override
    public ResponseResult getByUuid(String uuid) {
        if (StringUtils.isEmpty(uuid)){
            return ResponseResult.error();
        }
        //return ResponseResult.success().add("sysLog", this.baseMapper.selectOne(new QueryWrapper<SysLog>().eq("UUID", uuid)));
        return ResponseResult.success().add("sysLog", this.baseMapper.getByUuid(uuid));
    }

    @Override
    public IPage<SysLog> getSysLog(Page<SysLog> page, String logType) {
        if (logType.trim().length() == 0){
            return sysLogMapper.selectPage(page, new QueryWrapper<>());
        }else {
            return sysLogMapper.selectPage(page, new QueryWrapper<SysLog>().eq("LOG_TYPE", logType).orderByDesc("LOG_AUTO_ID"));
        }
    }

    @Override
    public Map<String, Object> getSysLogPage(int pageNo, int pageSize, String logType) {
        logType = logType == "" ? null : logType;
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> mapList = sysLogMapper.getAllSysLog(logType);
        PageInfo<Map<String, Object>> pageData = new PageInfo<>(mapList);
        Map<String,Object> pageInfo = new HashMap<>();
        pageInfo.put("records", pageData.getList());
        pageInfo.put("total", pageData.getTotal());
        pageInfo.put("size", pageData.getSize());
        pageInfo.put("current", pageData.getPageNum());
        pageInfo.put("pages", pageData.getPages());
        return pageInfo;
    }

    @Override
    public void insertSysLog(SysLog sysLog) {
        sysLogMapper.insert(sysLog);
    }

    @Override
    public void addSubmitOaRecord(String logType, String logContent, String operateType,
                                  String userId, String method, String requestUrl,
                                  String requestParam, String requestType, String remarks, String relateDocNumber) {
        String uuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
        SysLog sysLog = new SysLog();
        sysLog.setUuid(uuid);
        sysLog.setLogType(logType);
        sysLog.setLogContent(logContent);
        sysLog.setOperateType(operateType);
        sysLog.setUserId(userId);
        sysLog.setMethod(method);
        sysLog.setRequestUrl(requestUrl);
        sysLog.setRequestParam(requestParam);
        sysLog.setRequestType(requestType);
        sysLog.setRemarks(remarks);
        sysLog.setRelateDocNumber(relateDocNumber);
        sysLogMapper.insert(sysLog);
    }

    @Override
    public void spmsRestToSap(String logType, String logContent, String operateType,
                              String userId, String method, String requestParam,
                              String requestType, String remarks) {
        String uuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
        SysLog sysLog = new SysLog();
        sysLog.setUuid(uuid);
        sysLog.setLogType(logType);
        sysLog.setLogContent(logContent);
        sysLog.setOperateType(operateType);
        sysLog.setUserId(userId);
        sysLog.setMethod(method);
        sysLog.setRequestParam(requestParam);
        sysLog.setRequestType(requestType);
        sysLog.setRemarks(remarks);
        sysLogMapper.insert(sysLog);
    }

    @Override
    public void addSysLog(String logType, String logContent, String operateType, String userId, String ip,
                          String method, String requestUrl, String requestType, String relateDocNumber,
                          String requestParam, String remarks, String returnedMessage) {
        String uuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
        SysLog sysLog = new SysLog();
        sysLog.setUuid(uuid);
        sysLog.setLogType(logType);
        sysLog.setLogContent(logContent);
        sysLog.setOperateType(operateType);
        sysLog.setUserId(userId);
        sysLog.setUserId(ip);
        sysLog.setMethod(method);
        sysLog.setRequestParam(requestUrl);
        sysLog.setUserId(requestType);
        sysLog.setUserId(relateDocNumber);
        sysLog.setRequestParam(requestParam);
        sysLog.setRemarks(remarks);
        sysLog.setReturnedMessage(returnedMessage);
        sysLogMapper.insert(sysLog);
    }
}
